package com.softWang.file.util.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.softWang.common.exception.LogicException;
import com.softWang.file.mapper.FileManageMapper;
import com.softWang.file.po.FileManage;
import com.softWang.file.util.IFileSaveUtil;
import com.softWang.file.util.MimeTypeUtils;
import com.softWang.file.vo.fileManage.FileSaveResult;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;


@Component("FileSaveUtilQiniuImpl")
public  class FileSaveUtilQiniuImpl implements IFileSaveUtil {

    @Resource
    private FileManageMapper fileManageMapper;

    @Resource
    private ObjectMapper objectMapper;

    @Value("${qiniu.accesskey}")
    private  String ACCESS_KEY;
    @Value("${qiniu.secretkey}")
    private  String SECRET_KEY;
    // 要上传的空间
    @Value("${qiniu.bucketname}")
    private  String bucketName ;

    @Value("${qiniu.domain}")
    private  String QINIU_IMAGE_DOMAIN;

    @Value("${file.fileSaveType}")
    private  String fileSaveType;

    // 密钥配置
    private Auth auth = null;
    private UploadManager uploadManager =null;
    private BucketManager bucketManager =null;

    @PostConstruct
    private void init(){
        auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        //构造一个带指定Region对象的配置类
        com.qiniu.storage.Configuration cfg = new com.qiniu.storage.Configuration(Region.autoRegion());
        // ...其他参数参考类注释
        uploadManager = new UploadManager(cfg);
        bucketManager = new BucketManager(auth, cfg);
    }

    public String getUpToken() {
        return auth.uploadToken(bucketName);
    }


    @Override
    public FileSaveResult saveFile(MultipartFile file, String subDir, String fileName) throws Exception{
        // 调用put方法上传
        Response res = uploadManager.put(file.getBytes(),subDir+"/"+fileName, getUpToken());
        String url =null;
        // 打印返回的信息
        if (res.isOK() && res.isJson()) {
            // 返回这张存储照片的地址
            Map v = objectMapper.readValue(res.bodyString(), Map.class);
            url = "" + v.get("key");
        } else {
            throw  new LogicException("七牛异常:" + res.bodyString());
        }

        FileSaveResult result = new FileSaveResult();
        result.setFileUrl(url);
        return result;
    }

    @Override
    public String getFileUrl(FileManage baseFile) throws Exception {
        String fileUrl = QINIU_IMAGE_DOMAIN +  "/" + baseFile.getFileUrl();
        return fileUrl;
    }

    @Override
    public void deleteFile(FileManage baseFile) throws Exception {
        String filePath = baseFile.getFileDiskPath() + "/" + baseFile.getFileName();
        bucketManager.delete(bucketName,filePath);
    }


}
